<!DOCTYPE html>
<html>
<head>
</head>
<body>

	<!--Con//Start-->
	<div class="mar-st-a30">
		<form id="editdataForm" name="dataForm" method="post"
			class="form-horizontal" action="{{ctx}}/mam/appMain/update"
			enctype="multipart/form-data">
			<input type="hidden" name="id" readonly="readonly"
				value="{{appVo.id}}" id="appid" />
			<div class="mar-st-a30">
				<table width="100%" border="0" align="center" cellpadding="0"
					cellspacing="0" class="tab-td-pa">
					<tr>
						<td width="30%" align="right" for="focusedInput">应用标识：</td>
						<td class="bd_sr"><input class="form-control wid75"
							name="identifier" type="text" btvd-type="required"
							onblur="identifierCheck()" id="identifier"
							value="{{appVo.identifier}}"> <input type="hidden"
							name="showResult" readonly="readonly" value="" id="showResult" />
						</td>
					</tr>
					<tr>
						<td width="30%" align="right" for="focusedInput">应用名称：</td>
						<td class="bd_sr"><input class="form-control wid75"
							btvd-type="required" onblur="checkName()" id="app_name"
							name="app_name" type="text" value="{{appVo.app_name}}"> <input
							type="hidden" name="name_showResult" readonly="readonly"
							id="name_showResult" /></td>
					</tr>
					<tr>
						<td align="right" width="30%">应用类别：</td>
						<td class="bd_sr"><select name="categoryid"
							id="selectcategoryid" class="form-control wid75">
								{{#list}}
								<option value="{{id}}">{{name}}</option> {{/list}}
						</select></td>
					</tr>
					<tr>
						<td align="right" for="focusedInput" width="30%">应用图标：</td>
						<td class="bd_sr">
							<div id="iconUploadContainer"></div>
						</td>
					</tr>
					<tr>
						<td align="right" class="control-group" width="30%">应用简介：</td>
						<td class="bd_sr"><textarea id="discription"
								class="form-control wid75" name="discription" cols="" rows="">{{appVo.discription}}</textarea></td>
					</tr>
				</table>
			</div>
		</form>
	</div>
	<div class="hr-20"></div>
</body>
</html>

<script type="text/javascript">
	$("#selectcategoryid option[value='{{appVo.categoryid}}']").attr(
			"selected", true);
	$('#editdataForm').validation();
	//初始化上传组件
	$(function() {
		var uploader = buildPlupload({
			id : "iconUploadContainer",
			elmName : "icon",
			filters : [{title : "图片", extensions : "png"}],
			targetUrl : "{{ctx}}/mam/attachment/icon",
			tips:"请上传png格式图片",
			btnName : "上传图标",
			defaultVal : "{{appVo.icon}}"
		});
	});

	var popover = function(inputId, content) {
		var options = {
			placement : "bottom",
			content : content
		};
		$('#' + inputId).popover(options);
		$('#' + inputId).popover('show');
	}

	function formSubmit() {
		identifierCheck();
		checkName();
		var validateError=false;
		$('input, textarea').each(function() {
            var el = $(this), valid = el.attr('btvd-err');
            if (valid != null && valid.length > 0 ) {                       
            	validateError=true;
            	
            	return;
            }
        });
		
		if(validateError){
			return;
		}
		var data = $('#editdataForm').serialize();
		$.ajax({
			async : true,
			type : "POST",
			url : "{{ctx}}/mam/appMain/update",
			data : data,
			dataType : 'json',
			success : function(data, textStatus) {
				if (data.result == "success") {
					bootbox.alert("更新成功",function(){
						window.location.reload();
					});
				} else {
					bootbox.alert("更新失败，请稍等再试！");
				}
			},
			error : function(data, status, e) {
				alert(e);
			}
		});
	}

	function identifierCheck() {
		var oldId = "{{appVo.identifier}}";
		var identify = $("#identifier").val();
		
		if(oldId == identify){
			$("#identifier").removeAttr("btvd-type");
			$("#identifier").removeAttr("btvd-err");
			return;
		}
		
		if(identify==""){
			 $("#identifier").attr("btvd-type","required");
			 $("#identifier").attr("btvd-err","请输入内容");
				return;
		}
		
		var tel = /^[a-zA-Z]{1}([a-zA-Z0-9]+[.])+[a-zA-Z]{1}([A-z0-9]+)$/;
		var flag = tel.test(identify);
		if (!flag) {
			 $("#identifier").attr("btvd-err","必须以字母开头，只允许包含英文字母、数字和（.）字符。例如：com.foreveross.cube");
			 $("#identifier").attr("btvd-type","char");
			 return ;
		}else{
			$("#identifier").removeAttr("btvd-type");
			 $("#identifier").removeAttr("btvd-err");
		}
		
		$.ajax({
			async : false,
			type : "GET",
			dataType : "json",
			url : "{{ctx}}/mam/appMain/checkIdentify",
			data : "identify=" + identify,
			success : function(data, textStatus) {
				if (data.result == "true") {
					 $("#identifier").attr("btvd-type","char");
					 $("#identifier").attr("btvd-err","该应用标识ID已经被占用，请换别的试试！");
				} else {
					$("#identifier").removeAttr("btvd-type");
					$("#identifier").removeAttr("btvd-err");
				}

			},
		});
	}

	function checkName() {
		var oldName = "{{appVo.app_name}}";
		var name = $("#app_name").val();
		if(name == oldName){
			$("#app_name").removeAttr("btvd-type");
			$("#app_name").removeAttr("btvd-err");
			return;
		}
		if(name==""){
			 $("#app_name").attr("btvd-type","required");
			 $("#app_name").attr("btvd-err","请输入内容");
				return;
		}
		 var tel =/^[^`~!@#$%^&*()+=|\\\][\]\{\}:;'\,.<>/?]{1}[^`~!@$%^&()+=|\\\][\]\{\}:;'\,.<>?]{0,19}$/; 
		 var flag = tel.test(name);
		 if(!flag){
			 $("#app_name").attr("btvd-type","noSpecialChar");
			 $("#app_name").attr("btvd-err","应用名称不允许包含特殊字符，请换别的试试。");
			 return;
		 }
		$.ajax({
			async : false,
			type : "POST",
			dataType : "json",
			url : "{{ctx}}/mam/appMain/checkName",
			data : "checkName=" + name,
			success : function(data, textStatus) {
				if (data.result == "true") {
					 $("#app_name").attr("btvd-type","char");
					 $("#app_name").attr("btvd-err","该应用名称已经被占用，请换别的试试！");
				} else {
					$("#app_name").removeAttr("btvd-type");
					$("#app_name").removeAttr("btvd-err");
				}

			},
		});

	}
</script>
